public class Solution {
    public static void main(String[] args) {
        Solution test = new Solution();
        // int[] nums = new int[]{2,3,1,2,4,3};
        // System.out.println(test.minSubArrayLen(7, nums));
        System.out.println(test.lengthOfLongestSubstring("aaaa"));
    }

    public int minSubArrayLen(int target, int[] nums) {
        /**
         * 长度最小的子数组
         * right:连续子数组右边界（包含）
         * */
        // 1 预处理
        int n = nums.length;

        // 2 滑动窗口开始
        int left = 0;
        int right = -1;
        int minLength = 0x3f3f3f3f;
        int sum = 0;
        while(right < n-1) {
            // -每次步进1，先跳出死循环
            right ++;
            sum += nums[right];
            if(sum >= target) {
                // -将目前范围缩小至最小
                while(sum-nums[left] >= target) {
                    sum = sum-nums[left];
                    left ++;
                }
                // -更新范围
                int curLength = right - left + 1;
                if(curLength < minLength) {
                    minLength = curLength;
                }
            }
        }
        // 3 返回
        return minLength == 0x3f3f3f3f ? 0 : minLength;
    }

    public int lengthOfLongestSubstring(String s) {
        /**
         * 无重复字符的最长子串
         * 解法：滑动窗口优化，但好像又不全是滑动窗口...
         * */
        // 1 预处理
        int n = s.length();

        // 2 滑动窗口=>开滑
        int left = 0;
        int right = 0;
        int maxLength = 0;
        while(right < n) {
            // -若是当前插入字符存在窗口内，循环排除
            // -在[left, right-1]区间找到，即为true
            while(s.indexOf(s.charAt(right), left) < right && s.indexOf(s.charAt(right), left) >= left) {
                left ++;
            }
            // -走到此处必定能插入
            maxLength = Math.max(maxLength, right-left+1);
            right ++;
        }
        // 3 返回值
        return maxLength;
    }
}
